package com.cfcc.deptone.assetbarcodemgr.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.cfcc.deptone.assetbarcodemgr.R;
import com.cfcc.deptone.assetbarcodemgr.constant.AssetConstants;
import com.cfcc.deptone.assetbarcodemgr.entity.Product;
import com.cfcc.deptone.assetbarcodemgr.utils.PandianFileUtils;

import org.w3c.dom.Text;

import java.io.File;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AssetDetailActivity extends AppCompatActivity {


	private TextView codeTv;
	private TextView address;
	private TextView productName;
	private TextView buildArea;
	private TextView brand;
	private TextView model;
	private TextView passportNo;
	private TextView productNo;
	private TextView checkinStatus;
	private TextView pandianDate;
	private TextView assetTypeTv;
	private TextView remarkTv;
	private TextView softAssetsNameTv;
	private TextView versionnumTv;

	private Button checkConsistBtn;
	private Button checkNoConsistBtn;
	
	private Bundle bundle;
	private String filePath;

	public String getFilePath() {
		filePath = AssetDetailActivity.this.getFilesDir().getAbsoluteFile() + File.separator + AssetConstants.PANDIAN_TMPFILE_NAME;
		return filePath;
	}
	//private final AssetDetailRecvHandler handler = new AssetDetailRecvHandler(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_asset_detail);
		
		codeTv = (TextView) findViewById(R.id.code_tv);
        address = (TextView) findViewById(R.id.adress_tv);
    	productName = (TextView) findViewById(R.id.asset_tv);
    	buildArea = (TextView) findViewById(R.id.buildarea_tv); 
    	brand = (TextView) findViewById(R.id.brand_tv);
		model = (TextView) findViewById(R.id.model_tv);
    	passportNo = (TextView) findViewById(R.id.passport_tv);
    	productNo = (TextView) findViewById(R.id.productno_tv);
		checkinStatus = (TextView) findViewById(R.id.ispandian_tv);
    	pandianDate = (TextView) findViewById(R.id.pandian_date_tv);
		assetTypeTv = (TextView) findViewById(R.id.asset_type_tv);
		remarkTv = (TextView) findViewById(R.id.remark_tv);
		softAssetsNameTv = (TextView)findViewById(R.id.softAssetsName_tv);
		versionnumTv = (TextView) findViewById(R.id.versionnum_tv);

    	checkConsistBtn = (Button) findViewById(R.id.check_consist_btn);
    	checkNoConsistBtn = (Button) findViewById(R.id.check_no_consist_btn);
    	
    	TextView tvTitle = (TextView) findViewById(R.id.tv_title_asset_detail);

    	Intent intent = getIntent();
    	final String searchbyBarcode = intent.getExtras().getString(AssetConstants.SEARCHBYBARCODE);
    	final Bundle data = intent.getBundleExtra(AssetConstants.ASSET_RECORD);
		this.bundle = data;
    	initView(data);

		if(AssetConstants.FLG_PANDIAN_DETAIL.equals(searchbyBarcode)) {
			//从已盘点列表过来的
			tvTitle.setText(AssetConstants.PANDIAN_CHINESE+tvTitle.getText().toString());
		} else if(AssetConstants.FLG_NO_PANDIAN_DETAIL.equals(searchbyBarcode)) {
			//从盘点列表过来的
			tvTitle.setText(AssetConstants.NO_PANDIAN_CHINESE+tvTitle.getText().toString());
		} else {

		}
    	/*new Thread() {
			@Override
			public void run() {
				
				super.run();
				//bufferRead();
				//readRandom();
				PandianFileUtils.searchRecord(handler,searchbyBarcode);
			}

			
		}.start();*/
		
		
		checkConsistBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//一致
				/*if(AssetConstants.FLG_PANDIAN_DETAIL.equals(searchbyBarcode)) {
					//从已盘点列表过来的什么也不做直接返回已盘点页面
					AssetDetailActivity.this.finish();
				} else {
					//从未盘点列表过来的，核对一致的
					//从扫描查询过来的
					Intent intent = new Intent(AssetDetailActivity.this, AssetConsistActivity.class);
					intent.putExtra(AssetConstants.ASSET_RECORD, AssetDetailActivity.this.bundle);
					startActivity(intent);
				}*/
				Intent intent = new Intent(AssetDetailActivity.this, AssetConsistActivity.class);
				intent.putExtra(AssetConstants.ASSET_RECORD, AssetDetailActivity.this.bundle);
				intent.putExtra(AssetConstants.ASSET_CHECK_BTN_STATUS, AssetConstants.ASSET_CHECK_BTN_CONSIST);
				startActivity(intent);
			}
		});
		
		checkNoConsistBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*Intent intent = new Intent(AssetDetailActivity.this, AssetModifyActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);*/
				//不一致
				/*if(AssetConstants.FLG_PANDIAN_DETAIL.equals(searchbyBarcode)) {
					//从已盘点列表过来的核对不一致的，盘点状态改为0，备注信息清除，更新盘点日期

					final Product product = PandianFileUtils.buildProductWithBundle(data);

					//设置备注
					product.setRemarks(AssetConstants.STR_EMPTY);
					//设置当前时间
					SimpleDateFormat df = new SimpleDateFormat(AssetConstants.DATETIME_FORMAT);
					String strDateTime = df.format(new Date());
					product.setPandianDate(strDateTime);

					//将盘点类型改为盘点为1
					product.setCheckinStatus(AssetConstants.INIT_PANDIAN);
					//修改盘点状态
					PandianFileUtils.modifyByRandom(product, getFilePath());
					//通知用户盘点成功
					Toast.makeText(AssetDetailActivity.this, AssetConstants.MSG_RE_PANDIAN_SUCCESS, Toast.LENGTH_LONG).show();
					//迁移到主页面
					Intent intentAsset = new Intent(AssetDetailActivity.this, AssetActivity.class);
					intentAsset.putExtra(AssetConstants.PANDIAN_FLG, AssetConstants.PANDIAN);
					startActivity(intentAsset);
				} else if(AssetConstants.FLG_NO_PANDIAN_DETAIL.equals(searchbyBarcode)) {
					//从未盘点列表过来的，核对不一致的
					AssetDetailActivity.this.finish();
				} else {
					Intent intentMain = new Intent(AssetDetailActivity.this, MainActivity.class);
					startActivity(intentMain);
				}*/
				Intent intent = new Intent(AssetDetailActivity.this, AssetConsistActivity.class);
				intent.putExtra(AssetConstants.ASSET_RECORD, AssetDetailActivity.this.bundle);
				intent.putExtra(AssetConstants.ASSET_CHECK_BTN_STATUS, AssetConstants.ASSET_CHECK_BTN_NOCONSIST);
				startActivity(intent);
			}
		});
		
	}


	/**
	 * 初始化界面
	 * @param data
	 */
	private void initView(Bundle data) {
		String message = data.getString(AssetConstants.PRODUCT_COLNAME_BARCODE);
		//tv.setText(message);
		this.getCodeTv().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_PRODUCTNAME);
		this.getProductName().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_ADDRESS);
		this.getAddress().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_BUILDAREA);
		this.getBuildArea().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_BRAND);
		this.getBrand().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_MODEL);
		this.getModel().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_PASSPORTNO);
		this.getPassportNo().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_PRODUCTNO);
		this.getProductNo().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_PANDIANFLG);
		this.getCheckinStatus().setText(message);
		message = data.getString(AssetConstants.PRODUCT_COLNAME_PANDIANDATE);
		this.getPandianDate().setText(message);
		String assetType = data.getString(AssetConstants.PRODUCT_COLNAME_ASSETTYPE);
		this.getAssetTypeTv().setText(assetType);

		String softAssetsName = data.getString(AssetConstants.PRODUCT_COLNAME_SOFTASSETSNAME);
		this.getSoftAssetsNameTv().setText(softAssetsName);

		String versionnum = data.getString(AssetConstants.PRODUCT_COLNAME_VERSIONNUM);
		this.getVersionnumTv().setText(versionnum);

		/*TableRow tr = (TableRow) findViewById(R.id.tr_asset_type);
		tr.setVisibility(View.GONE);*/

		message = data.getString(AssetConstants.PRODUCT_COLNAME_REMARK);
		this.getRemarkTv().setText(message);

        //按照资产类型初始化界面
        initTypeView(assetType);
		//this.setBundle(msg.getData());
	}


    /**
     * 按照资产类型初始化界面
     * @param assetType
     */
	private void initTypeView(String assetType) {


	    //条形码
        TableRow trCode = (TableRow) findViewById(R.id.tr_code);
        trCode.setVisibility(View.VISIBLE);

        //资产名称
        TableRow trAsset = (TableRow) findViewById(R.id.tr_asset);
        trAsset.setVisibility(View.VISIBLE);

        //备注
        TableRow trRemark = (TableRow) findViewById(R.id.tr_remark);
        trRemark.setVisibility(View.VISIBLE);

	    if(AssetConstants.ASSET_TYPE_BUILDING.equals(assetType)) {
            //房屋及建筑物

            //地址
            TableRow trAddress = (TableRow) findViewById(R.id.tr_adress);
            trAddress.setVisibility(View.VISIBLE);
            //建筑面积
            TableRow trBuildArea = (TableRow) findViewById(R.id.tr_buildarea);
            trBuildArea.setVisibility(View.VISIBLE);

        } else if(AssetConstants.ASSET_TYPE_ELEC.equals(assetType)
                || AssetConstants.ASSET_TYPE_MECH.equals(assetType)
                || AssetConstants.ASSET_TYPE_OTHER.equals(assetType)) {
	        //电子设备 机械器具 其他资产

            //品牌
            TableRow trBrand = (TableRow) findViewById(R.id.tr_brand);
            trBrand.setVisibility(View.VISIBLE);
            //型号
            TableRow trModel = (TableRow) findViewById(R.id.tr_model);
            trModel.setVisibility(View.VISIBLE);
            //出厂编号
            TableRow trProductNo = (TableRow) findViewById(R.id.tr_productno);
            trProductNo.setVisibility(View.VISIBLE);

        } else if(AssetConstants.ASSET_TYPE_CONVEY.equals(assetType)) {
            //运输工具

            //品牌
            TableRow trBrand = (TableRow) findViewById(R.id.tr_brand);
            trBrand.setVisibility(View.VISIBLE);
            //型号
            TableRow trModel = (TableRow) findViewById(R.id.tr_model);
            trModel.setVisibility(View.VISIBLE);
            //车辆牌照
            TableRow trPassport = (TableRow) findViewById(R.id.tr_passport);
            trPassport.setVisibility(View.VISIBLE);
        } else if(AssetConstants.ASSET_TYPE_SOFT.equals(assetType)) {
	    	//软件

			//软件资产具体名称
			TableRow trSoftAssetsName = (TableRow) findViewById(R.id.tr_softAssetsName);
			trSoftAssetsName.setVisibility(View.VISIBLE);
			//版本号
			TableRow trVersionnum = (TableRow) findViewById(R.id.tr_versionnum);
			trVersionnum.setVisibility(View.VISIBLE);
		}



    }

	@Override
	public void onBackPressed() {
		//可以屏蔽回退键
		super.onBackPressed();
	}

	/**
	 * 防止内存泄漏启用弱引用
	 */
	/*private static class AssetDetailRecvHandler extends Handler {

		private final WeakReference<AssetDetailActivity> mActivity;

		public AssetDetailRecvHandler(AssetDetailActivity activity) {
			mActivity = new WeakReference<AssetDetailActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			AssetDetailActivity _activity = mActivity.get();
			if(_activity != null) {
				String message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_BARCODE);
				//tv.setText(message);
				_activity.getCodeTv().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_PRODUCTNAME);
				_activity.getProductName().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_ADDRESS);
				_activity.getAddress().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_BUILDAREA);
				_activity.getBuildArea().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_BRAND);
				_activity.getBrand().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_MODEL);
				_activity.getModel().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_PASSPORTNO);
				_activity.getPassportNo().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_PRODUCTNO);
				_activity.getProductNo().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_PANDIANFLG);
				_activity.getCheckinStatus().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_PANDIANDATE);
				_activity.getPandianDate().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_ASSETTYPE);
				_activity.getAssetTypeTv().setText(message);
				message = msg.getData().getString(AssetConstants.PRODUCT_COLNAME_REMARK);
				_activity.getRemarkTv().setText(message);
				_activity.setBundle(msg.getData());
			}
		}
	}*/

	public TextView getCodeTv() {
		return codeTv;
	}

	public TextView getAddress() {
		return address;
	}

	public TextView getProductName() {
		return productName;
	}

	public TextView getBuildArea() {
		return buildArea;
	}

	public TextView getBrand() {
		return brand;
	}

	public TextView getModel() {
		return model;
	}

	public TextView getPassportNo() {
		return passportNo;
	}

	public TextView getProductNo() {
		return productNo;
	}


	public TextView getPandianDate() {
		return pandianDate;
	}

	public Bundle getBundle() {
		return bundle;
	}

	public void setBundle(Bundle bundle) {
		this.bundle = bundle;
	}

	public TextView getCheckinStatus() {
		return checkinStatus;
	}

	public TextView getAssetTypeTv() {
		return assetTypeTv;
	}

	public TextView getRemarkTv() {
		return remarkTv;
	}

	public TextView getSoftAssetsNameTv() {
		return softAssetsNameTv;
	}

	public void setSoftAssetsNameTv(TextView softAssetsNameTv) {
		this.softAssetsNameTv = softAssetsNameTv;
	}

	public TextView getVersionnumTv() {
		return versionnumTv;
	}

	public void setVersionnumTv(TextView versionnumTv) {
		this.versionnumTv = versionnumTv;
	}
}
